<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="联系电话" prop="phone">
				<el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="会员号" prop="meId">
				<el-input v-model="queryParams.meId" placeholder="请输入会员号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="会员等级" prop="grade">
				<el-select v-model="queryParams.grade" placeholder="请选择会员等级" clearable>
					<el-option v-for="dict in bus_rem_level" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="生日" prop="birthday">
				<el-date-picker clearable v-model="queryParams.birthday" type="date" value-format="YYYY-MM-DD"
					placeholder="请选择生日">
				</el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleAdd"
					v-hasPermi="['crm:memberInfo:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Operation" :disabled="single" @click="handleUpdateGrade"
					v-hasPermi="['crm:memberInfo:grade']">调整等级</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
					v-hasPermi="['crm:memberInfo:remove']">删除</el-button>
			</el-col>
			<!-- <el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport"
					v-hasPermi="['crm:memberInfo:export']">导出</el-button>
			</el-col> -->
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="memberInfoList" style="height: calc(100vh - 330px);" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="ID" align="center" prop="id" />
			<el-table-column label="姓名" align="center" prop="name" />
			<el-table-column label="性别" align="center" prop="gender">
				<template #default="scope">
					<dict-tag :options="bus_gender" :value="scope.row.gender" />
				</template>
			</el-table-column>
			<el-table-column label="联系电话" align="center" prop="phone">
				<template #default="scope">
					<el-link type="primary" @click="showPhone(scope.row)">{{scope.row.phone}}</el-link>
				</template>
			</el-table-column>
			<el-table-column label="会员号" align="center" prop="meId" />
			<el-table-column label="会员等级" align="center" prop="grade">
				<template #default="scope">
					<dict-tag :options="bus_rem_level" :value="scope.row.grade" />
				</template>
			</el-table-column>
			<el-table-column label="会员折扣" align="center" prop="discount" />
			<el-table-column label="生日" align="center" prop="birthday" width="180">
				<template #default="scope">
					<span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="积分" align="center" prop="integral" />
			<el-table-column label="储值余额" align="center" prop="storedAmount">
				<template #default="scope">
					
					<el-dropdown trigger="click">
						<span class="el-dropdown-link">
						  {{scope.row.storedAmount}}<el-icon class="el-icon--right"><arrow-down /></el-icon>
						</span>
						<template #dropdown>
						  <el-dropdown-menu>
							<el-dropdown-item @click="handleStored(scope.row,'1')" :icon="Plus">储值</el-dropdown-item>
							<el-dropdown-item @click="handleStored(scope.row,'2')" divided :icon="CirclePlusFilled">消费</el-dropdown-item>
							<el-dropdown-item @click="handleOpenRecord(scope.row)" divided :icon="CirclePlus">使用记录</el-dropdown-item>
						  </el-dropdown-menu>
						</template>
					  </el-dropdown>
					
						
				</template>
			</el-table-column>
			<!-- <el-table-column label="外部积分" align="center" prop="extIntegral" />
      <el-table-column label="外部会员号" align="center" prop="extMeId" />
      <el-table-column label="归属员工" align="center" prop="belUserId" />
      <el-table-column label="归属店铺" align="center" prop="belDeptId" /> -->
			<el-table-column label="注册方式" align="center" prop="regWay">
				<template #default="scope">
					<dict-tag :options="bus_reg_way" :value="scope.row.regWay" />
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template #default="scope">
					<el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['crm:memberInfo:edit']">修改</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改会员信息对话框 -->
		<el-dialog :title="title" v-model="open" width="500px" append-to-body close-on-click-modal="false">
			<el-form ref="memberInfoRef" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="称呼" prop="name">
					<el-input v-model="form.name" placeholder="请输入称呼" />
				</el-form-item>
				<el-form-item label="性别" prop="gender">
					<el-select v-model="form.gender" placeholder="请选择性别">
						<el-option v-for="dict in bus_gender" :key="dict.value" :label="dict.label"
							:value="dict.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="联系电话" prop="phone" v-if="form.id==null">
					<el-input v-model="form.phone" placeholder="请输入联系电话" />
				</el-form-item>
				<!-- <el-form-item label="会员号" prop="meId">
					<el-input v-model="form.meId" placeholder="请输入会员号" />
				</el-form-item> -->
				<!-- <el-form-item label="会员等级" prop="grade">
					<el-select v-model="form.grade" placeholder="请选择会员等级">
						<el-option v-for="dict in bus_rem_level" :key="dict.value" :label="dict.label"
							:value="dict.value"></el-option>
					</el-select>
				</el-form-item> -->
				<el-form-item label="生日" prop="birthday">
					<el-date-picker clearable v-model="form.birthday" type="date" value-format="YYYY-MM-DD"
						placeholder="请选择生日">
					</el-date-picker>
				</el-form-item>
				<!-- <el-form-item label="外部积分" prop="extIntegral">
          <el-input v-model="form.extIntegral" placeholder="请输入外部积分" />
        </el-form-item>
        <el-form-item label="外部会员号" prop="extMeId">
          <el-input v-model="form.extMeId" placeholder="请输入外部会员号" />
        </el-form-item> -->

			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
		
		
		<el-dialog title="调整会员等级" v-model="gradeOpen" width="300px" append-to-body close-on-click-modal="false">
			<el-form ref="gradeFormRef" :model="gradeForm" :rules="gradeRules" label-width="80px">
				<el-form-item label="会员等级" prop="grade">
					<el-select v-model="gradeForm.grade" placeholder="请选择会员等级">
						<el-option v-for="dict in bus_rem_level" :key="dict.value" :label="dict.label"
							:value="dict.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="setChangeGrade">调 整</el-button>
					<el-button @click="gradeOpen=false">取 消</el-button>
				</div>
			</template>
		</el-dialog>
		
		<diffStoredAmount ref="diffStoredAmountRef" @success="successStored"/>
		
		<storedRecord ref="storedRecordRef"/>
		
	</div>
</template>

<script setup name="MemberInfo">
	import {
		listMemberInfo,
		getMemberInfo,
		delMemberInfo,
		addMemberInfo,
		updateMemberInfo,
		getPhone,
		changeGrade
	} from "@/api/crm/memberInfo";
	import {  ElMessageBox} from 'element-plus'
	
	import diffStoredAmount from "./diffStoredAmount.vue";
	
	import storedRecord from '../storedRecord/index.vue'

	const diffStoredAmountRef = ref();
	
	const storedRecordRef = ref();
	
	let authPwd = false;
	
	const {
		proxy
	} = getCurrentInstance();
	const {
		bus_gender,
		bus_rem_level,
		bus_reg_way
	} = proxy.useDict('bus_gender', 'bus_rem_level', 'bus_reg_way');

	const memberInfoList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	
	const gradeOpen = ref(false)
	

	const data = reactive({
		form: {},
		gradeForm:{
			id:null,
			grade:null
		},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			name: null,
			gender: null,
			phone: null,
			meId: null,
			grade: null,
			birthday: null,
			integral: null,
			extIntegral: null,
			extMeId: null,
			belUserId: null,
			belDeptId: null,
			regWay: null,
		},
		rules: {
			name: [{
				required: true,
				message: '请输入称呼',
				trigger: 'blur'
			}, ],
			gender: [{
				required: true,
				message: '请选择性别',
				trigger: 'change'
			}, ],
			phone: [{
				required: true,
				message: '请输入联系电话',
				trigger: 'blur'
			}, {
				pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
				message: "请输入正确的手机号码",
				trigger: "blur"
			}],
			birthday: [{
				required: true,
				message: '请选择生日',
				trigger: 'change'
			}, ],
		},
		gradeRules:{
			grade: [{
				required: true,
				message: '请选择会员等级',
				trigger: 'change'
			}, ],
		}
	});

	const {
		queryParams,
		form,
		rules,
		gradeForm,
		gradeRules
	} = toRefs(data);
	
	

	/** 查询会员信息列表 */
	function getList() {
		loading.value = true;
		listMemberInfo(queryParams.value).then(response => {
			memberInfoList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			id: null,
			name: null,
			gender: null,
			phone: null,
			meId: null,
			grade: null,
			birthday: null,
			integral: null,
			extIntegral: null,
			extMeId: null,
			belUserId: null,
			belDeptId: null,
			regWay: null,
			createBy: null,
			createTime: null,
			updateBy: null,
			updateTime: null
		};
		proxy.resetForm("memberInfoRef");
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.id);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加会员信息";
	}

	/** 调整会员等级 */
	function handleUpdateGrade(row) {
		const _id = row.id || ids.value[0]
		
		gradeForm.value.id = _id;
		gradeOpen.value = true;
	}
	
	function setChangeGrade(){
		proxy.$refs["gradeFormRef"].validate(valid => {
			if (valid) {
				changeGrade(gradeForm.value.id,gradeForm.value.grade).then(rsp=>{
					proxy.$modal.msgSuccess("调整成功");
					gradeOpen.value = false;
					getList();
				})
			}
		})
		
	}
	
	
	/** 修改按钮操作 */
	function handleUpdate(row) {
	  reset();
	  const _id = row.id || ids.value
	  getMemberInfo(_id).then(response => {
	    form.value = response.data;
	    open.value = true;
	    title.value = "修改会员信息";
	  });
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["memberInfoRef"].validate(valid => {
			if (valid) {
				if (form.value.id != null) {
					updateMemberInfo(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addMemberInfo(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const _ids = row.id || ids.value;
		proxy.$modal.confirm('是否确认删除会员信息编号为"' + _ids + '"的数据项？').then(function() {
			return delMemberInfo(_ids);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('crm/memberInfo/export', {
			...queryParams.value
		}, `memberInfo_${new Date().getTime()}.xlsx`)
	}
	
	
	//点击储值
	function handleStored(row,type){
		diffStoredAmountRef.value.openWin(row,type)
	}
	
	//储值后回调
	function successStored(value){
		getList();
	}
	
	function handleOpenRecord(row){
		storedRecordRef.value.openWin(row.meId);
	}
	
	
	function showPhone(row){
		if(authPwd){
			getPhone(row.id).then(rsp=>{
				proxy.$modal.alert(rsp.data,'联系电话')
			})
		}else{
			ElMessageBox.prompt('请输入授权密码查看数据', "授权提示", {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			  type: "warning",
			  inputType:'password'
			}).then(({ value }) => {
				if('cy123888'===value){
					authPwd = true;
					
					getPhone(row.id).then(rsp=>{
						proxy.$modal.alert(rsp.data,'联系电话')
					})
					
					setTimeout(()=>{
						authPwd = false;
					},1000*60*3)
					
				}else{
					proxy.$modal.msgWarning('无效授权密码.')
				}
			}).catch(() => {
			})
		}
		
		
		
		
	}
	
	getList();
</script>